package com.controller;

import com.model.CarModel;
import com.services.ICarServices;
import com.services.impl.CarServicesImpl;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Program: exam
 * @ClassName: CarServletController
 * @Description: Add and Edit car operations
 * @Author: li
 */
@WebServlet("/car/save")
public class CarServletController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        Map<String, Object> result = new HashMap<>();
        
        try {
            // Get form parameters
            String cidStr = request.getParameter("cid");
            String cname = request.getParameter("cname");
            String cinfo = request.getParameter("cinfo");
            String cimg = request.getParameter("cimg");
            String crentStr = request.getParameter("crent");
            String statusStr = request.getParameter("status");

            // Parameter validation
            if (cname == null || cname.trim().isEmpty()) {
                result.put("code", 400);
                result.put("msg", "车辆名称不能为空");
                response.getWriter().write(JSONObject.fromObject(result).toString());
                return;
            }

            if (cinfo == null || cinfo.trim().isEmpty()) {
                result.put("code", 400);
                result.put("msg", "车辆信息不能为空");
                response.getWriter().write(JSONObject.fromObject(result).toString());
                return;
            }

            if (cimg == null || cimg.trim().isEmpty()) {
                result.put("code", 400);
                result.put("msg", "车辆图片不能为空");
                response.getWriter().write(JSONObject.fromObject(result).toString());
                return;
            }

            // Parse numeric parameters
            double crent;
            try {
                crent = Double.parseDouble(crentStr);
                if (crent < 0) {
                    result.put("code", 400);
                    result.put("msg", "租金不能小于0");
                    response.getWriter().write(JSONObject.fromObject(result).toString());
                    return;
                }
            } catch (NumberFormatException e) {
                result.put("code", 400);
                result.put("msg", "租金格式不正确");
                response.getWriter().write(JSONObject.fromObject(result).toString());
                return;
            }

            int status = statusStr != null ? Integer.parseInt(statusStr) : 1;

            // Create carModel object
            CarModel carModel = new CarModel();
            if (cidStr != null && !cidStr.trim().isEmpty()) {
                carModel.setCid(Integer.parseInt(cidStr));
            }
            carModel.setCname(cname);
            carModel.setCinfo(cinfo);
            carModel.setCimg(cimg);
            carModel.setCrent(crent);
            carModel.setStatus(status);
            
            // Call service to add or update carModel
            ICarServices carServices = new CarServicesImpl();
            if (carModel.getCid() != null) {
                carServices.updateCar(carModel);
                result.put("msg", "更新成功");
            } else {
                carServices.addCar(carModel);
                result.put("msg", "添加成功");
            }
            
            result.put("code", 0);
            
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "操作失败：" + e.getMessage());
        }
        
        response.getWriter().write(JSONObject.fromObject(result).toString());
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        doPost(request, response);
    }
}
